package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.self.GoodsListEntity3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsListActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsListModel3 extends BaseViewModel {
    public ObservableField<String> condition = new ObservableField<>("");
    public ObservableField<String> status = new ObservableField<>("");
    public ObservableField<String> cid3 = new ObservableField<>("");
    private GoodsListActivity3 activity;
    private SelfRepertory repertory;
    private int size = 10;
    private int page = 1;
    public int total;

    public GoodsListModel3(GoodsListActivity3 activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //获取商品列表
    public void getGoodsListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        List<String> cid3List = null;
        if (!cid3.get().equals("")) {
            cid3List = new ArrayList<>();
            cid3List.add(cid3.get());
        }
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID,"");
        DialogHelper.showProgressDialog(activity, null, "数据请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", size);
        map.put("text", condition.get());
        map.put("status", status.get());
        if (cid3List != null && cid3List.size() > 0) {
            map.put("cid3", cid3List);
        }
        map.put("companyId", companyId);
        NewAPIManager.getInstance().getGoodsReleasedAPI().myGoodsList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<GoodsListEntity3>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<GoodsListEntity3>>> data) {
                activity.showList(data.getData().getList(), isLoadMore);
            }
        });
    }

    //获取商品列表全部条件
    public void getGoodsAllModel() {
        NewAPIManager.getInstance().getGoodsReleasedAPI().goodsStatusCategory().enqueue(new ICallBack<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onSuccess(BaseEntity<List<DictEntity3>> data) {
                activity.showAllPopWindow(data.getData());
            }
        });
    }

    //获取分类类别
    public void getGoodsCategoryModel() {
        NewAPIManager.getInstance().getGoodsReleasedAPI().materialGoodsCategory(repertory.getAreaCode()).enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    activity.showCategoryView(data.getData());
                }
            }
        });
    }

    //上下架删除商品
    public void upDowngetGoodsModel(String opt, List<String> goodsIds) {
        Map map = new HashMap();
        map.put("status", opt);
        map.put("goodsIds", goodsIds);
        NewAPIManager.getInstance().getGoodsReleasedAPI().upDown(map).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                activity.showOptSuccess(opt, goodsIds.size());
            }
        });
    }

}
